package work.droit.data;

import work.droit.data.Champs.CategorieChamp;
import work.util.data.table.Tables.Champ.SqlType;
import work.util.data.table.Tables.Record;

public class Categorie extends Record<CategorieChamp>
{
	public static CategorieChamp	id			= new CategorieChamp("id", SqlType.INT, true);
	public static CategorieChamp	parentId	= new CategorieChamp("parentId", SqlType.INT);
	public static CategorieChamp	name		= new CategorieChamp("name", SqlType.VARCHAR50);

	public static String			TABLENAME	= "categorie";

	public Categorie()
	{
		super(TABLENAME);
		setChamp(id, null);
		setChamp(parentId, null);
		setChamp(name, "");
	}

	public static String getCompleteCategorieName(Number id)
	{
		StringBuffer sb = new StringBuffer();
		Number currentId = id;
		while (currentId != null && currentId.intValue() != 0)
		{
			Categorie currentCat = new Categorie();
			currentCat.setChamp(Categorie.id, currentId);
			currentCat.load();
			sb.append(currentCat.getValue(Categorie.name));
			sb.append(" /");
			currentId = (Number) currentCat.getValue(Categorie.parentId);
		}
		return sb.toString();
	}

	@Override
	public boolean delete()
	{
		Fiche fiche = new Fiche();
		fiche.setConnection(connection);
		fiche.setChamp(Fiche.categorie, getValue(Categorie.id));
		Categorie categorie = new Categorie();
		categorie.setConnection(connection);
		categorie.setChamp(Categorie.parentId, getValue(Categorie.id));
		if (super.delete())
		{
			fiche.delete(Fiche.categorie);
			categorie.delete(Categorie.parentId);
			return true;
		}
		return false;
	}
}